﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using USMCdotNET;

namespace USMC.Net
{
    /// <summary>
    /// Interaction logic for manualParametersPage.xaml
    /// </summary>
    public partial class manualParametersPage : baseParametersPage
    {
        public manualParametersPage(Devices devices)
            :base()
        {
            InitializeComponent();
            _devices = devices;
            //WTF????
            //xdisablebuttonscheckbox.ischecked = _devices.MODE[0].PMode;
            //ydisablebuttonscheckbox.ischecked = _devices.MODE[0].PMode;
            xResetDelayTextBox.Text = _devices.PRMS[0].BTimeoutR.ToString("F3");
            yResetDelayTextBox.Text = _devices.PRMS[1].BTimeoutR.ToString("F3");
            xImpulseButton1.IsChecked = _devices.MODE[0].Butt1T;
            xImpulseButton2.IsChecked = _devices.MODE[0].Butt2T;
            yImpulseButton1.IsChecked = _devices.MODE[1].Butt1T;
            yImpulseButton2.IsChecked = _devices.MODE[1].Butt2T;
        }
        public override void ApplyParameters()
        {
            
            if (xDisableButtonsCheckBox.IsChecked == true)
                _devices.MODE[0].PMode = false;
            else
                _devices.MODE[0].PMode = true;

            if (yDisableButtonsCheckBox.IsChecked == true)
                _devices.MODE[1].PMode = false;
            else
                _devices.MODE[1].PMode = true;
            try
            {
                float bTx=float.Parse(xResetDelayTextBox.Text);
                if (bTx > 0)
                    _devices.PRMS[0].BTimeoutR = bTx;
                else
                    MessageBox.Show("Неверная задержка сброса по X", "Ошибка");
            }
            catch
            {
                MessageBox.Show("Неверная задержка сброса по X", "Ошибка");
            }
            try
            {
                float bTy = float.Parse(yResetDelayTextBox.Text);
                if (bTy > 0)
                    _devices.PRMS[1].BTimeoutR = bTy;
                else
                    MessageBox.Show("Неверная задержка сброса по X", "Ошибка");
            }
            catch
            {
                MessageBox.Show("Неверная задержка сброса по X", "Ошибка");
            }
            if (xImpulseButton1.IsChecked == true)
                _devices.MODE[0].Butt1T = true;
            else
                _devices.MODE[0].Butt1T = false;
            if (xImpulseButton2.IsChecked == true)
                _devices.MODE[0].Butt2T = true;
            else
                _devices.MODE[0].Butt2T = false;
            if (yImpulseButton1.IsChecked == true)
                _devices.MODE[1].Butt1T = true;
            else
                _devices.MODE[1].Butt1T = false;
            if (yImpulseButton2.IsChecked == true)
                _devices.MODE[1].Butt2T = true;
            else
                _devices.MODE[1].Butt2T = false;
        }
        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox t = (TextBox)sender;
            char ch = (char)e.Key;
            if (!Char.IsDigit(ch))
            {
                if (e.Key != Key.Back && e.Key != Key.Delete)
                    e.Handled = true;
                else if (t.Text.Length == 1 || t.SelectedText.Length == t.Text.Length)
                    e.Handled = true;
            }
            else if (t.Text.Length > 5)
            {
                if (t.SelectedText.Length == 0)
                    e.Handled = true;
            }
        }
    }
}
